﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace Enterprise.Web
{
    public class SiteUtility
    {
        private static string skinPath = "Themes/";
        /// <summary>
        /// 获取Css列表
        /// </summary>
        /// <returns></returns>
        public static string[] GetCssFileUrlList()
        {
            XDocument configXml = XDocument.Load(HttpContext.Current.Server.MapPath("~/Config/WebSkin.xml"));
            XElement cfgRoot = configXml.Root;
            string defaultPath = cfgRoot.Attribute("defaultPath").Value;
            string loadCssPath = cfgRoot.Attribute("loadCssPath").Value;
            string cssFileNames = cfgRoot.Attribute("cssFileNames").Value;

            List<string> cssFileList = new List<string>();
            SpliceCssPath(defaultPath, ref cssFileList, loadCssPath, cssFileNames);
            return  cssFileList.ToArray();
        }
        /// <summary>
        /// 根据风格路径,拼接cssURL列表
        /// </summary>
        /// <param name="cssPath">风格目录名</param>
        /// <param name="cssList">找到的css需要加到这里去</param>
        /// <param name="loadCssPath">风格包中css的目录(将载入这个css目录中的所有css文件)</param>
        public static void SpliceCssPath(string cssPath, ref List<string> cssList, string loadCssPath, string cssFileNames)
        {
            string[] cssFileNameArr = cssFileNames.Split(',');
            foreach (string cssFileName in cssFileNameArr)
            {
                string temCssAllPath = skinPath + cssPath + "/" + loadCssPath + "/" + cssFileName + ".css";
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/" + temCssAllPath)))
                {
                    cssList.Add(temCssAllPath);
                }
            }
        }
        /// <summary>
        /// 获取资源根路径
        /// </summary>
        /// <returns></returns>
        public static string GetResRootPath()
        {
            XDocument configXml = XDocument.Load(HttpContext.Current.Server.MapPath("~/Config/WebSkin.xml"));
            XElement cfgRoot = configXml.Root;
            string defaultPath ="Themes/"+ cfgRoot.Attribute("defaultPath").Value+"/";
            return defaultPath;
        }
    }

}
